package fnord.network.calculator;

import fnord.illuminati.action.AttackModifierAction;
import fnord.illuminati.card.*;
import fnord.illuminati.order.*;

public class DefenseCalculator extends Calculator {
	
	private int _defensePower;
	private AttackOrder _ao;
	
	public DefenseCalculator(GroupCard defender, Alignment[] oppAlignments, AttackType at, AttackOrder ao) {
		//TODO: calculate the distance of the defender from its illuminati, then the allignment stuff,
		//TODO: and then factor in power/resistance and neutralization numbers based on at
		_defensePower = 0;
		_ao = ao;
		
		switch(at) {
		case NEUTRALIZE:
			_defensePower -= 6;
		case CONTROL:
			_defensePower += defender.getResistance();
			for(Alignment myAlignment : defender.getAlignments()) {
				for(Alignment theirAlignment : oppAlignments) {
					if(myAlignment.getAlliedType() == theirAlignment) {
						_defensePower -= 4;
					}
					else if(myAlignment.getOpposingType() == theirAlignment) {
						_defensePower += 4;
					}
				}
			}
			break;
		case DESTROY:
			_defensePower += defender.getPower();
			for(Alignment myAlignment : defender.getAlignments()) {
				for(Alignment theirAlignment : oppAlignments) {
					if(myAlignment.getAlliedType() == theirAlignment) {
						_defensePower += 4;
					}
					else if(myAlignment.getOpposingType() == theirAlignment) {
						_defensePower -= 4;
					}
				}
			}
		}
		
		GroupCard depthCard = defender;
		int depth = 0;
		while(depthCard != null && !(depthCard instanceof IlluminatiCard)) {
			depthCard = depthCard.getParent();
			depth++;
		}
		if(depthCard != null) {
			switch(depth) {
			case 1:
				_defensePower += 10;
				break;
			case 2:
				_defensePower += 5;
				break;
			case 3:
				_defensePower += 2;
				break;
			default:
				break;
			}
			//at this point, depthcard is definitely an illuminati!!
			
			traverse((IlluminatiCard) depthCard);
		}
		
	}
	
	@Override
	public void calculate(GroupCard toCalculateOn) {
		Object action = toCalculateOn.getAction();
		if(action instanceof AttackModifierAction) {
			_defensePower -= ((AttackModifierAction) action).doDefend(_ao);
		}

	}
	
	public int getDefenseValue() { return _defensePower; }
	
	/*public int defenseValue(GroupCard defending) {
		//TODO: return the defense value for the card
		//defending.getResistance() + 
		return _defensePower;
	}*/

}
